package controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import pojo.Conversation;
import service.ConversationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对话管理控制器
 * 提供医患对话的CRUD操作及查询接口
 */
@Api(tags = "对话管理API", description = "医患对话相关接口")
@RestController
@RequestMapping("/api/conversation")
public class ConversationController {

    @Autowired
    private ConversationService conversationService;

    /**
     * 根据用户ID和医生ID查询对话记录
     * @param userId 用户ID
     * @param doctorId 医生ID
     * @return 对话记录列表
     */
    @ApiOperation(value = "查询用户与医生的对话记录", notes = "根据用户ID和医生ID获取双方的所有对话，按时间倒序排列")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "Integer", paramType = "path"),
            @ApiImplicitParam(name = "doctorId", value = "医生ID", required = true, dataType = "String", paramType = "path")
    })
    @GetMapping("/user/{userId}/doctor/{doctorId}")
    public Map<String, Object> getByUserIdAndDoctorId(@PathVariable Integer userId, @PathVariable String doctorId) {
        Map<String, Object> result = new HashMap<>();
        List<Conversation> conversations = conversationService.getByUserIdAndDoctorId(userId, doctorId);
        result.put("code", 200);
        result.put("message", "查询成功");
        result.put("data", conversations);
        return result;
    }

    /**
     * 根据ID查询对话详情
     * @param id 对话ID
     * @return 对话详情
     */
    @ApiOperation(value = "根据ID查询对话", notes = "通过对话ID获取具体对话内容")
    @ApiImplicitParam(name = "id", value = "对话ID", required = true, dataType = "Integer", paramType = "path")
    @GetMapping("/{id}")
    public Map<String, Object> getById(@PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        Conversation conversation = conversationService.getById(id);
        if (conversation != null) {
            result.put("code", 200);
            result.put("message", "查询成功");
            result.put("data", conversation);
        } else {
            result.put("code", 404);
            result.put("message", "未找到对应对话");
        }
        return result;
    }

    /**
     * 查询用户的所有对话记录
     * @param userId 用户ID
     * @return 对话记录列表
     */
    @ApiOperation(value = "查询用户的所有对话", notes = "获取指定用户的全部对话记录，按时间倒序排列")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "Integer", paramType = "path")
    @GetMapping("/user/{userId}")
    public Map<String, Object> getByUserId(@PathVariable Integer userId) {
        Map<String, Object> result = new HashMap<>();
        List<Conversation> conversations = conversationService.getByUserId(userId);
        result.put("code", 200);
        result.put("message", "查询成功");
        result.put("data", conversations);
        return result;
    }

    /**
     * 查询医生的所有对话记录
     * @param doctorId 医生ID
     * @return 对话记录列表
     */
    @ApiOperation(value = "查询医生的所有对话", notes = "获取指定医生的全部对话记录，按时间倒序排列")
    @ApiImplicitParam(name = "doctorId", value = "医生ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/doctor/{doctorId}")
    public Map<String, Object> getByDoctorId(@PathVariable String doctorId) {
        Map<String, Object> result = new HashMap<>();
        List<Conversation> conversations = conversationService.getByDoctorId(doctorId);
        result.put("code", 200);
        result.put("message", "查询成功");
        result.put("data", conversations);
        return result;
    }

    /**
     * 添加新对话记录
     * @param conversation 对话实体
     * @return 添加结果
     */
    @ApiOperation(value = "添加新对话", notes = "创建一条新的医患对话记录")
    @PostMapping("/add")
    public Map<String, Object> add(@RequestBody Conversation conversation) {
        Map<String, Object> result = new HashMap<>();
        int count = conversationService.add(conversation);
        if (count > 0) {
            result.put("code", 200);
            result.put("message", "添加成功");
            result.put("data", conversation.getId());
        } else {
            result.put("code", 500);
            result.put("message", "添加失败");
        }
        return result;
    }

    /**
     * 更新对话记录
     * @param conversation 对话实体（需包含ID）
     * @return 更新结果
     */
    @ApiOperation(value = "更新对话", notes = "修改已有对话的内容")
    @PutMapping("/update")
    public Map<String, Object> update(@RequestBody Conversation conversation) {
        Map<String, Object> result = new HashMap<>();
        int count = conversationService.update(conversation);
        if (count > 0) {
            result.put("code", 200);
            result.put("message", "更新成功");
        } else {
            result.put("code", 500);
            result.put("message", "更新失败");
        }
        return result;
    }

    /**
     * 删除对话记录
     * @param id 对话ID
     * @return 删除结果
     */
    @ApiOperation(value = "删除对话", notes = "根据ID删除指定的对话记录")
    @ApiImplicitParam(name = "id", value = "对话ID", required = true, dataType = "Integer", paramType = "path")
    @DeleteMapping("/delete/{id}")
    public Map<String, Object> delete(@PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        int count = conversationService.delete(id);
        if (count > 0) {
            result.put("code", 200);
            result.put("message", "删除成功");
        } else {
            result.put("code", 500);
            result.put("message", "删除失败");
        }
        return result;
    }
}